package com.letsiot.applications.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.letsiot.applications.dto.SysUserDTO;
import com.letsiot.applications.entity.SysUser;
import com.letsiot.applications.entity.SysUserAuths;
import com.letsiot.applications.service.SysUserService;

@RestController
@RequestMapping("/user")
@Validated
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @PostMapping("save")
    public int save(@RequestBody SysUser entity) {
        return sysUserService.save(entity);
    }

    @DeleteMapping("delete")
    public int delete(Long id) {
        return sysUserService.delete(id);
    }

    @PutMapping("update")
    public int update(@RequestBody SysUser entity) {
        return sysUserService.update(entity);
    }

    @GetMapping("findById")
    public SysUser findById(Long id) {
        return sysUserService.findById(id);
    }

    @GetMapping("getAll")
    public List<SysUser> getAll() {
        return sysUserService.getAll();
    }

    @GetMapping("getUserByIdentifier")
    public SysUser getUserByIdentifier(String identifier) {
        return sysUserService.getUserByIdentifier(identifier);
    }

    @GetMapping("getUserAuthByIdentifier")
    public SysUserAuths getUserAuthByIdentifier(String identifier) {
        return sysUserService.getUserAuthByIdentifier(identifier);
    }

    @GetMapping("getUserAuthByUserId")
    public List<SysUserAuths> getUserAuthByUserId(Long userId) {
        return sysUserService.getUserAuthByUserId(userId);
    }

    @GetMapping("getUserPage")
    public Page<SysUser> getUserPage(int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        return sysUserService.getUserPage(pageable);
    }

    @PostMapping("createUser")
    public void createUser(@RequestBody SysUserDTO userVO) {
        sysUserService.createUser(userVO);
    }

    @DeleteMapping("deleteUserById")
    public boolean deleteUserById(Long userId) {
        return sysUserService.delete(userId) > 0 ? true : false;
    }

    @PutMapping("updateByPrimaryKeySelective")
    public boolean updateByPrimaryKeySelective(@RequestBody SysUser user) {
        return sysUserService.update(user) > 0 ? true : false;
    }

    @GetMapping("selectByPrimaryKey")
    public SysUser selectByPrimaryKey(Long userId) {
        return sysUserService.findById(userId);
    }

    @PostMapping("login")
    public boolean login(@RequestBody SysUser user) {
        return sysUserService.login(user);
    }

    @PostMapping("logout")
    public boolean logout(@RequestBody SysUser user) {
        return sysUserService.logout(user);
    }

    @PutMapping("updatePassword")
    public boolean updatePassword(@RequestBody SysUserDTO userVO) {
        return sysUserService.updatePassword(userVO) > 0 ? true : false;
    }
}
